home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 April: Mac OS SDK / Dev.CD Apr 99 SDK1.toast / Development Kits / QuickTime / QuickTime 3 Interfaces & Libs / QTDevWin / CIncludes / Endian.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-21  |  12.0 KB  |  428 lines  |  [TEXT/dosa]

  1. /*
  2.      File:        Endian.h
  3.  
  4.      Contains:    QuickTime Interfaces
  5.  
  6.      Version:    Technology:    QuickTime 3.0
  7.                  Release:    QuickTime 3.0
  8.  
  9.      Copyright:    © 1997-1998 by Apple Computer, Inc., all rights reserved
  10.  
  11.      Bugs?:        Please include the the file and version information (from above) with
  12.                  the problem description.  Developers belonging to one of the Apple
  13.                  developer programs can submit bug reports to:
  14.  
  15.                      devsupport@apple.com
  16.  
  17. */
  18. #ifndef __ENDIAN__
  19. #define __ENDIAN__
  20.  
  21. #ifndef __CONDITIONALMACROS__
  22. #include <ConditionalMacros.h>
  23. #endif
  24. #ifndef __MACTYPES__
  25. #include <MacTypes.h>
  26. #endif
  27.  
  28.  
  29.  
  30. #if PRAGMA_ONCE
  31. #pragma once
  32. #endif
  33.  
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37.  
  38. #if PRAGMA_IMPORT
  39. #pragma import on
  40. #endif
  41.  
  42. #if PRAGMA_STRUCT_ALIGN
  43.     #pragma options align=mac68k
  44. #elif PRAGMA_STRUCT_PACKPUSH
  45.     #pragma pack(push, 2)
  46. #elif PRAGMA_STRUCT_PACK
  47.     #pragma pack(2)
  48. #endif
  49.  
  50. /*
  51.     This file provides Endian Flipping routines for dealing with converting data
  52.     between Big-Endian and Little-Endian machines.  These routines are useful
  53.     when writing code to compile for both Big and Little Endian machines and  
  54.     which must handle other endian number formats, such as reading or writing 
  55.     to a file or network packet.
  56.     
  57.     These routines are named as follows:
  58.     
  59.         Endian<U><W>_<S>to<D>
  60.  
  61.     where
  62.         <U>    is whether the integer is signed ('S') or unsigned ('U')
  63.         <W> is integer bit width: 16, 32, or 64 
  64.         <S> is the source endian format: 'B' for big, 'L' for little, or 'N' for native
  65.         <D> is the destination endian format: 'B' for big, 'L' for little, or 'N' for native
  66.     
  67.     For example, to convert a Big Endian 32-bit unsigned integer to the current native format use:
  68.         
  69.         long i = EndianU32_BtoN(data);
  70.         
  71.     This file is set up so that the function macro to nothing when the target runtime already
  72.     is the desired format (e.g. on Big Endian machines, EndianU32_BtoN() macros away).
  73.             
  74.     If long long's are not supported, you cannot get 64-bit quantities as a single value.
  75.     The macros are not defined in that case.
  76.     
  77.     
  78.     
  79.                                 <<< W A R N I N G >>>
  80.     
  81.     It is very important not to put any autoincrements inside the macros.  This 
  82.     will produce erroneous results because each time the address is accessed in the macro, 
  83.     the increment occurs.
  84.     
  85.  */
  86. EXTERN_API_C( SInt16 )
  87. EndianS16_BtoN                    (SInt16                 value);
  88.  
  89. EXTERN_API_C( SInt16 )
  90. EndianS16_NtoB                    (SInt16                 value);
  91.  
  92. EXTERN_API_C( SInt16 )
  93. EndianS16_LtoN                    (SInt16                 value);
  94.  
  95. EXTERN_API_C( SInt16 )
  96. EndianS16_NtoL                    (SInt16                 value);
  97.  
  98. EXTERN_API_C( SInt16 )
  99. EndianS16_LtoB                    (SInt16                 value);
  100.  
  101. EXTERN_API_C( SInt16 )
  102. EndianS16_BtoL                    (SInt16                 value);
  103.  
  104. EXTERN_API_C( UInt16 )
  105. EndianU16_BtoN                    (UInt16                 value);
  106.  
  107. EXTERN_API_C( UInt16 )
  108. EndianU16_NtoB                    (UInt16                 value);
  109.  
  110. EXTERN_API_C( UInt16 )
  111. EndianU16_LtoN                    (UInt16                 value);
  112.  
  113. EXTERN_API_C( UInt16 )
  114. EndianU16_NtoL                    (UInt16                 value);
  115.  
  116. EXTERN_API_C( UInt16 )
  117. EndianU16_LtoB                    (UInt16                 value);
  118.  
  119. EXTERN_API_C( UInt16 )
  120. EndianU16_BtoL                    (UInt16                 value);
  121.  
  122. EXTERN_API_C( SInt32 )
  123. EndianS32_BtoN                    (SInt32                 value);
  124.  
  125. EXTERN_API_C( SInt32 )
  126. EndianS32_NtoB                    (SInt32                 value);
  127.  
  128. EXTERN_API_C( SInt32 )
  129. EndianS32_LtoN                    (SInt32                 value);
  130.  
  131. EXTERN_API_C( SInt32 )
  132. EndianS32_NtoL                    (SInt32                 value);
  133.  
  134. EXTERN_API_C( SInt32 )
  135. EndianS32_LtoB                    (SInt32                 value);
  136.  
  137. EXTERN_API_C( SInt32 )
  138. EndianS32_BtoL                    (SInt32                 value);
  139.  
  140. EXTERN_API_C( UInt32 )
  141. EndianU32_BtoN                    (UInt32                 value);
  142.  
  143. EXTERN_API_C( UInt32 )
  144. EndianU32_NtoB                    (UInt32                 value);
  145.  
  146. EXTERN_API_C( UInt32 )
  147. EndianU32_LtoN                    (UInt32                 value);
  148.  
  149. EXTERN_API_C( UInt32 )
  150. EndianU32_NtoL                    (UInt32                 value);
  151.  
  152. EXTERN_API_C( UInt32 )
  153. EndianU32_LtoB                    (UInt32                 value);
  154.  
  155. EXTERN_API_C( UInt32 )
  156. EndianU32_BtoL                    (UInt32                 value);
  157.  
  158. EXTERN_API_C( SInt64 )
  159. EndianS64_BtoN                    (SInt64                 value);
  160.  
  161. EXTERN_API_C( SInt64 )
  162. EndianS64_NtoB                    (SInt64                 value);
  163.  
  164. EXTERN_API_C( SInt64 )
  165. EndianS64_LtoN                    (SInt64                 value);
  166.  
  167. EXTERN_API_C( SInt64 )
  168. EndianS64_NtoL                    (SInt64                 value);
  169.  
  170. EXTERN_API_C( SInt64 )
  171. EndianS64_LtoB                    (SInt64                 value);
  172.  
  173. EXTERN_API_C( SInt64 )
  174. EndianS64_BtoL                    (SInt64                 value);
  175.  
  176. EXTERN_API_C( UInt64 )
  177. EndianU64_BtoN                    (UInt64                 value);
  178.  
  179. EXTERN_API_C( UInt64 )
  180. EndianU64_NtoB                    (UInt64                 value);
  181.  
  182. EXTERN_API_C( UInt64 )
  183. EndianU64_LtoN                    (UInt64                 value);
  184.  
  185. EXTERN_API_C( UInt64 )
  186. EndianU64_NtoL                    (UInt64                 value);
  187.  
  188. EXTERN_API_C( UInt64 )
  189. EndianU64_LtoB                    (UInt64                 value);
  190.  
  191. EXTERN_API_C( UInt64 )
  192. EndianU64_BtoL                    (UInt64                 value);
  193.  
  194. /*
  195.    These types are used for structures that contain data that is
  196.    always in BigEndian format.  This extra typing prevents little
  197.    endian code from directly changing the data, thus saving much
  198.    time in the debugger.
  199. */
  200. #if TARGET_RT_LITTLE_ENDIAN
  201.  
  202. struct BigEndianLong {
  203.     long                             bigEndianValue;
  204. };
  205. typedef struct BigEndianLong            BigEndianLong;
  206.  
  207. struct BigEndianUnsignedLong {
  208.     unsigned long                     bigEndianValue;
  209. };
  210. typedef struct BigEndianUnsignedLong    BigEndianUnsignedLong;
  211.  
  212. struct BigEndianShort {
  213.     short                             bigEndianValue;
  214. };
  215. typedef struct BigEndianShort            BigEndianShort;
  216.  
  217. struct BigEndianUnsignedShort {
  218.     unsigned short                     bigEndianValue;
  219. };
  220. typedef struct BigEndianUnsignedShort    BigEndianUnsignedShort;
  221.  
  222. struct BigEndianFixed {
  223.     Fixed                             bigEndianValue;
  224. };
  225. typedef struct BigEndianFixed            BigEndianFixed;
  226.  
  227. struct BigEndianUnsignedFixed {
  228.     UnsignedFixed                     bigEndianValue;
  229. };
  230. typedef struct BigEndianUnsignedFixed    BigEndianUnsignedFixed;
  231.  
  232. struct BigEndianOSType {
  233.     OSType                             bigEndianValue;
  234. };
  235. typedef struct BigEndianOSType            BigEndianOSType;
  236. #else
  237.  
  238. typedef long                             BigEndianLong;
  239. typedef unsigned long                     BigEndianUnsignedLong;
  240. typedef short                             BigEndianShort;
  241. typedef unsigned short                     BigEndianUnsignedShort;
  242. typedef Fixed                             BigEndianFixed;
  243. typedef UnsignedFixed                     BigEndianUnsignedFixed;
  244. typedef OSType                             BigEndianOSType;
  245. #endif  /* TARGET_RT_LITTLE_ENDIAN */
  246.  
  247.  
  248. /*
  249.     Macro away no-op functions
  250. */
  251. #if TARGET_RT_BIG_ENDIAN
  252.     #define EndianS16_BtoN(value)                (value)
  253.     #define EndianS16_NtoB(value)                (value)
  254.     #define EndianU16_BtoN(value)                (value)
  255.     #define EndianU16_NtoB(value)                (value)
  256.     #define EndianS32_BtoN(value)                (value)
  257.     #define EndianS32_NtoB(value)                (value)
  258.     #define EndianU32_BtoN(value)                (value)
  259.     #define EndianU32_NtoB(value)                (value)
  260.     #define EndianS64_BtoN(value)                (value)
  261.     #define EndianS64_NtoB(value)                (value)
  262.     #define EndianU64_BtoN(value)                (value)
  263.     #define EndianU64_NtoB(value)                (value)
  264. #else
  265.     #define EndianS16_LtoN(value)                (value)
  266.     #define EndianS16_NtoL(value)                (value)
  267.     #define EndianU16_LtoN(value)                (value)
  268.     #define EndianU16_NtoL(value)                (value)
  269.     #define EndianS32_LtoN(value)                (value)
  270.     #define EndianS32_NtoL(value)                (value)
  271.     #define EndianU32_LtoN(value)                (value)
  272.     #define EndianU32_NtoL(value)                (value)
  273.     #define EndianS64_LtoN(value)                (value)
  274.     #define EndianS64_NtoL(value)                (value)
  275.     #define EndianU64_LtoN(value)                (value)
  276.     #define EndianU64_NtoL(value)                (value)
  277. #endif
  278.  
  279.  
  280.  
  281. /*
  282.     Map native to actual
  283. */
  284. #if TARGET_RT_BIG_ENDIAN
  285.     #define EndianS16_LtoN(value)                EndianS16_LtoB(value)
  286.     #define EndianS16_NtoL(value)                EndianS16_BtoL(value)
  287.     #define EndianU16_LtoN(value)                EndianU16_LtoB(value)
  288.     #define EndianU16_NtoL(value)                EndianU16_BtoL(value)
  289.     #define EndianS32_LtoN(value)                EndianS32_LtoB(value)
  290.     #define EndianS32_NtoL(value)                EndianS32_BtoL(value)
  291.     #define EndianU32_LtoN(value)                EndianU32_LtoB(value)
  292.     #define EndianU32_NtoL(value)                EndianU32_BtoL(value)
  293.     #define EndianS64_LtoN(value)                EndianS64_LtoB(value)
  294.     #define EndianS64_NtoL(value)                EndianS64_BtoL(value)
  295.     #define EndianU64_LtoN(value)                EndianU64_LtoB(value)
  296.     #define EndianU64_NtoL(value)                EndianU64_BtoL(value)
  297. #else
  298.     #define EndianS16_BtoN(value)                EndianS16_BtoL(value)
  299.     #define EndianS16_NtoB(value)                EndianS16_LtoB(value)
  300.     #define EndianU16_BtoN(value)                EndianU16_BtoL(value)
  301.     #define EndianU16_NtoB(value)                EndianU16_LtoB(value)
  302.     #define EndianS32_BtoN(value)                EndianS32_BtoL(value)
  303.     #define EndianS32_NtoB(value)                EndianS32_LtoB(value)
  304.     #define EndianU32_BtoN(value)                EndianU32_BtoL(value)
  305.     #define EndianU32_NtoB(value)                EndianU32_LtoB(value)
  306.     #define EndianS64_BtoN(value)                EndianS64_BtoL(value)
  307.     #define EndianS64_NtoB(value)                EndianS64_LtoB(value)
  308.     #define EndianU64_BtoN(value)                EndianU64_BtoL(value)
  309.     #define EndianU64_NtoB(value)                EndianU64_LtoB(value)
  310. #endif
  311.  
  312.  
  313.  
  314. /*
  315.     Implement ≈LtoB and ≈BtoL
  316. */
  317. #define EndianS16_LtoB(value)                ((SInt16)Endian16_Swap(value))
  318. #define EndianS16_BtoL(value)                ((SInt16)Endian16_Swap(value))
  319. #define EndianU16_LtoB(value)                ((UInt16)Endian16_Swap(value))
  320. #define EndianU16_BtoL(value)                ((UInt16)Endian16_Swap(value))
  321. #define EndianS32_LtoB(value)                ((SInt32)Endian32_Swap(value))
  322. #define EndianS32_BtoL(value)                ((SInt32)Endian32_Swap(value))
  323. #define EndianU32_LtoB(value)                ((UInt32)Endian32_Swap(value))
  324. #define EndianU32_BtoL(value)                ((UInt32)Endian32_Swap(value))
  325. #define EndianS64_LtoB(value)                ((SInt64)Endian64_Swap((UInt64)value))
  326. #define EndianS64_BtoL(value)                ((SInt64)Endian64_Swap((UInt64)value))
  327. #define EndianU64_LtoB(value)                ((UInt64)Endian64_Swap(value))
  328. #define EndianU64_BtoL(value)                ((UInt64)Endian64_Swap(value))
  329.  
  330.  
  331.  
  332. /*
  333.     Implement low level ≈_Swap functions.
  334.     
  335.         extern UInt16 Endian16_Swap(UInt16 value);
  336.         extern UInt32 Endian32_Swap(UInt32 value);
  337.         extern UInt64 Endian64_Swap(UInt64 value);
  338.         
  339.     Note: Depending on the processor, you might want to implement
  340.           these as function calls instead of macros.
  341.     
  342. */
  343.  
  344. #if TARGET_CPU_68K && TARGET_OS_MAC
  345.     #pragma parameter __D0 Endian16_Swap(__D0)
  346.     pascal UInt16 Endian16_Swap(UInt16 value)
  347.         = { 0xE158 };
  348.     
  349.     #pragma parameter __D0 Endian32_Swap (__D0)
  350.     pascal UInt32 Endian32_Swap(UInt32 value)
  351.         = { 0xE158, 0x4840, 0xE158 };
  352. #else
  353.     #define Endian16_Swap(value)                 \
  354.             (((((UInt16)value)<<8) & 0xFF00)   | \
  355.              ((((UInt16)value)>>8) & 0x00FF))
  356.     
  357.     #define Endian32_Swap(value)                     \
  358.             (((((UInt32)value)<<24) & 0xFF000000)  | \
  359.              ((((UInt32)value)<< 8) & 0x00FF0000)  | \
  360.              ((((UInt32)value)>> 8) & 0x0000FF00)  | \
  361.              ((((UInt32)value)>>24) & 0x000000FF))
  362. #endif
  363.  
  364.  
  365. #if _LONG_LONG
  366.     #ifdef __MRC__
  367.         #define Endian64_Swap(value)                             \
  368.                 (((((UInt64)value)<<56) & 0xFF00000000000000ULL)  | \
  369.                  ((((UInt64)value)<<40) & 0x00FF000000000000ULL)  | \
  370.                  ((((UInt64)value)<<24) & 0x0000FF0000000000ULL)  | \
  371.                  ((((UInt64)value)<< 8) & 0x000000FF00000000ULL)  | \
  372.                  ((((UInt64)value)>> 8) & 0x00000000FF000000ULL)  | \
  373.                  ((((UInt64)value)>>24) & 0x0000000000FF0000ULL)  | \
  374.                  ((((UInt64)value)>>40) & 0x000000000000FF00ULL)  | \
  375.                  ((((UInt64)value)>>56) & 0x00000000000000FFULL))
  376.     #else
  377.         #define Endian64_Swap(value)                             \
  378.                 (((((UInt64)value)<<56) & 0xFF00000000000000)  | \
  379.                  ((((UInt64)value)<<40) & 0x00FF000000000000)  | \
  380.                  ((((UInt64)value)<<24) & 0x0000FF0000000000)  | \
  381.                  ((((UInt64)value)<< 8) & 0x000000FF00000000)  | \
  382.                  ((((UInt64)value)>> 8) & 0x00000000FF000000)  | \
  383.                  ((((UInt64)value)>>24) & 0x0000000000FF0000)  | \
  384.                  ((((UInt64)value)>>40) & 0x000000000000FF00)  | \
  385.                  ((((UInt64)value)>>56) & 0x00000000000000FF))
  386.     #endif
  387. #else
  388.     /* 
  389.         Note: When using C compilers that don't support "long long",
  390.               Endian64_Swap must be implemented as glue. 
  391.     */
  392.     #ifdef __cplusplus
  393.         inline static UInt64 Endian64_Swap(UInt64 value)
  394.         {
  395.             UInt64 temp;
  396.             temp.lo = Endian32_Swap(value.hi);
  397.             temp.hi = Endian32_Swap(value.lo);
  398.             return temp;
  399.         }
  400.     #else
  401.         extern UInt64 Endian64_Swap(UInt64 value);
  402.     #endif
  403. #endif
  404.  
  405.  
  406.  
  407.  
  408. #if PRAGMA_STRUCT_ALIGN
  409.     #pragma options align=reset
  410. #elif PRAGMA_STRUCT_PACKPUSH
  411.     #pragma pack(pop)
  412. #elif PRAGMA_STRUCT_PACK
  413.     #pragma pack()
  414. #endif
  415.  
  416. #ifdef PRAGMA_IMPORT_OFF
  417. #pragma import off
  418. #elif PRAGMA_IMPORT
  419. #pragma import reset
  420. #endif
  421.  
  422. #ifdef __cplusplus
  423. }
  424. #endif
  425.  
  426. #endif /* __ENDIAN__ */
  427.  
  428.